home *** CD-ROM | disk | FTP | other *** search
- Path: nntp.teleport.com!usenet
- From: GHouck <hksys@teleport.com>
- Newsgroups: comp.lang.c
- Subject: Re: changing strings via pointers
- Date: 23 Feb 1996 21:47:38 GMT
- Organization: systems hk
- Message-ID: <4glclq$ovu@maureen.teleport.com>
- References: <1996Feb22.125436.25503@leeds.ac.uk>
- NNTP-Posting-Host: ip-pdx04-40.teleport.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
-
- csyamc@scs.leeds.ac.uk (A M Casey) wrote:
- [snip]
- >The thing is, I want to get rid of the "\n" and replace it with "\0", but
- >obviously I can only do this using the pointer. I'm using the following code:
- >
- >char *Contents < pointer to string
- >int count = 0;
- >
- >while (*(Contents + count)!= NULL) /* the end of string is not reached */
- >{if (*(Contents + count) == atoi("\n"))
- >/* change the \n to a \0 and set the next char along to null */
- >{
- >*(Contents + count) = atoi("\0");
- >
- >*(Contents + count + 1) = NULL;
- >}
- >else
- >/* read in the next char */
- >{
- >count++;
- >
- >I think the problem maybe my way of using the pointer - I can print out each
- >char as I get to it, but it doesnt seem to change the array at all.
- >Should I be using atoi?, it doesnt seem to work without it.
- >
- [snip]
- Andy,
- Assuming that you have allocated memory for 'Contents', I would
- suggest the following alternative to strip new-lines (no loop
- required):
-
- ...
- char *pnl;
- ...
- if( (pnl=strchr(Contents,'\n')) )
- *pnl = '\000';
- ...
-
- Or change your use of '...== atoi('\n') to that of simply testing each
- character with '\n':
- ...
- {if (*(Contents + count) == '\n') )
- ...
-
- Yours, Geoff Houck
-
-